define(function(require,exports,module){
	require('masonry');
	var common = require("common");
	exports.init = function(){
		//瀑布流效果
		masonry_list();
		//鼠标hover商品上的放大效果
		mouse_hover_food();
		//click_to_add_to_car
		click_to_add_to_car();
	}	

	exports.operate = function(){
	}	
	exports.win_resize = function(){
	}

	function mouse_hover_food(){
		$(".food_item").live('mouseenter',function(){
			var left = parseInt($(this).css("left"));
			var top = parseInt($(this).css("top"));
			$(this).css("height","280px");
			$(this).css("top",top-10 + "px");
			$(this).find(".food_info").show();
			$(this).find(".food_operate").show();
		}).live("mouseleave",function(){
			var top = parseInt($(this).css("top"));
			$(this).css("height","150px");
			$(this).css("top",top+10 + "px");
			$(this).find(".food_info").hide();
			$(this).find(".food_operate").hide();
		});
	}

	function masonry_list(){
		var food_container = $('#food_container');
		food_container.masonry({
		    itemSelector : '.food_item',
		    isAnimated: true
		});
		var loading = $("#loading").data("on", false);
		$(window).scroll(function(){
		    if(loading.data("on")) return;
		    //注释：scrollTop:比如说上下的滚动条，当滚动条下拉是，上面隐藏的部分问scrollTop
		    //$(document).height()：整个文档的高度
		    //$(window).height()：可是区域的高度
		    //$('.footer').height()：底部元素的高度
		    if($(document).scrollTop() > $(document).height()-$(window).height()-$('.footer').height()){//页面拖到底部了
		        //加载更多数据
		        loading.data("on", true).fadeIn();         //在这里将on设为true来阻止继续的ajax请求
		        var to_data = {
		        	pageNum : loading.find('span').attr("page_num"),
		        	goodsType:loading.find('span').attr("goods_type")
		        }
		        $.get(
		            url.masonry_url, 
		            to_data,
		            function(data){
		               //获取到了数据data,后面用JS将数据新增到页面上
		               if(!data.status){
		            		loading.remove();
		            		return;
		            	}
		               var goods_data = data.data.data;
		                var html = "";
		                if($.isArray(goods_data)){
		                	loading.find('span').attr('page_num',data.data.pageNum);
		                    for (var i = goods_data.length - 1; i >= 0; i--) {
		                        html += " <div class='food_item'><img class='list_pic_show' src='"+goods_data[i]['goods_pic']+"' alt='商品'><div class='food_info'><span>"+goods_data[i]['goods_name']+"</span></div><div class='food_operate'><button class='btn btn-primary'>添加购物车</button><button class='btn btn-success'>立即购买</button></div></div>";
		                    }
		                    var $newElems = $(html).css({ opacity: 0 }).appendTo(food_container);
		                  
	                        $newElems.animate({ opacity: 1 });
	                        food_container.masonry( 'appended', $newElems, true ); 
		                         
		                  //一次请求完成，将on设为false，可以进行下一次的请求
		                    loading.data("on", false);
		                }
		                loading.fadeOut();
		            },"json");
		    }
    	});		
	}

	function click_to_add_to_car(){
		$(".foodlist_add_to_car").live('click',function(){
			var goods_id = $(this).parents(".food_item").attr('goods-id');
			var data = {
				goods_id:goods_id,
				goods_num:1
			}
			$.post(url.foodAddToCarUrl,data,function(e){
				if(e.status){
					var cookData = common.getCookie("myCar");
					var cookArr = cookData.split(':');
					$(".user_car font").text(cookArr[1]);
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'成功',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});	   				
	   			}else{
					$.Zebra_Dialog(e.message, {
					    'type':'errot',
					    'title':'失败',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});	   				
	   			}
			},'json');
		});
	}
});